package com.powerunion.datacollection.dao.impl;

import java.sql.Connection;

import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.UrgencyDao;
import com.powerunion.datacollection.model.Urgency;
import com.powerunion.datacollection.persistence.db.jdbc.operator.QueryOperator;
import com.powerunion.datacollection.persistence.db.jdbc.operator.RsDataSet;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlParameter;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlType;
import com.powerunion.datacollection.persistence.db.jdbc.operator.UpdateOperator;
import com.powerunion.util.log.LogHelper;

public class UrgencyDaoImpl extends JdbcBase implements UrgencyDao {
	// 初始化日志类
	private static final Logger logger = LogHelper.getInstance().getLogger(
			UrgencyDaoImpl.class);

	public boolean delete(String procId) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug(Constants.INTOMETHOD + "delete");
		}
		String sql = "update dc_urgency set del_flag = 1 where proc_id = ? ";
		UpdateOperator update = new UpdateOperator();
		update.setSql(sql);
		update.addParameter(new SqlParameter(SqlType.VARCHAR, procId));

		try {
			update.access();
		} catch (Exception e) {
			logger.error("", e);
			throw new DaoException("delete throw of error!");
		}

		int effectCount = update.getEffectCounts();
		if (effectCount > 0) {
			if (logger.isDebugEnabled()) {
				logger.debug("delete:" + Constants.DELETE + "催办信息"
						+ Constants.SUCCESS);
			}
			return true;
		} else {
			if (logger.isDebugEnabled()) {
				logger.debug("delete:" + Constants.DELETE + "催办信息"
						+ Constants.FAIL);
			}
			return false;
		}

	}

	public String save(Urgency urgency, Connection conn) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug(Constants.INTOMETHOD + "save");
		}
//		logger.debug("urgency info: procId="+urgency.getProcId()+"||actId="+urgency.getActId()+"||urgeTime="+urgency.getUrgeTime());
//		logger.debug("||urgencyContent="+urgency.getUrgencyContent()+"||remindInfo="+urgency.getRemindInfo()+"||repeatConut="+urgency.getRepeatCount());
//		logger.debug("||RepeatIntervalDay="+urgency.getRepeatIntervalDay()+"||RepeatIntervalHour="+urgency.getRepeatIntervalHour()+"||type="+urgency.getType());
//		logger.debug("||id="+urgency.getId());
		
		String sql = "";
		String resultId = "";

		String id = String.valueOf(this.getSeq("dc_urgency"));
		urgency.setId(id);
		StringBuffer sqlSave = new StringBuffer();
		sqlSave
				.append(
						"insert into dc_urgency (proc_id,act_id,urge_time,urge_info,remind_info,repeat_count,")
				.append(
						"repeat_interval_day,repeat_interval_hour,type,id) values(?,?,?,?,?,?,?,?,?,?)");
		sql = sqlSave.toString();

		UpdateOperator update = new UpdateOperator();
		update.setUseOnlyConnectionFlag(true);
		update.setPresentConnection(conn);
		update.setSql(sql);
		update.addParameter(new SqlParameter(SqlType.VARCHAR, urgency
				.getProcId()));
		update.addParameter(new SqlParameter(SqlType.VARCHAR, urgency
				.getActId()));
		update.addParameter(new SqlParameter(SqlType.VARCHAR, urgency
				.getUrgeTime()));
		update.addParameter(new SqlParameter(SqlType.VARCHAR, urgency
				.getUrgencyContent()));
		update.addParameter(new SqlParameter(SqlType.VARCHAR, urgency
				.getRemindInfo()));
		update.addParameter(new SqlParameter(SqlType.INTEGER, new Integer(
				urgency.getRepeatCount())));
		update.addParameter(new SqlParameter(SqlType.INTEGER, new Integer(
				urgency.getRepeatIntervalDay())));
		update.addParameter(new SqlParameter(SqlType.INTEGER, new Integer(
				urgency.getRepeatIntervalHour())));
		update.addParameter(new SqlParameter(SqlType.INTEGER, new Integer(
				urgency.getType())));
		update.addParameter(new SqlParameter(SqlType.VARCHAR, urgency.getId()));
		try {
			update.access();
		} catch (Exception e) {
			logger.error("", e);
			throw new DaoException("保存催办信息出错！");
		}

		int effectCount = update.getEffectCounts();
		if (effectCount > 0) {

			if (logger.isDebugEnabled()) {
				logger.debug("save:" + Constants.SAVE + "催办信息"
						+ Constants.SUCCESS);
			}
			resultId = urgency.getId();
			return resultId;
		} else {
			if (logger.isDebugEnabled()) {
				logger
						.debug("save:" + Constants.SAVE + "催办信息"
								+ Constants.FAIL);
			}
			return "";
		}
	}

	public Urgency getBykey(String actId, Connection conn) throws DaoException {
		Urgency urgency = new Urgency();
		StringBuffer sql = new StringBuffer();
		sql
				.append(
						"select id,proc_id,act_id,urge_time,urge_info,remind_info,del_flag,repeat_count, ")
				.append(
						"repeat_interval_day,repeat_interval_hour from dc_urgency where act_id = ? ");
		QueryOperator query = new QueryOperator();
		query.setUseOnlyConnectionFlag(true);
		query.setPresentConnection(conn);
		query.setSql(sql.toString());
		query.addParameter(new SqlParameter(SqlType.VARCHAR, actId));
		try {
			query.access();
		} catch (Exception e) {
			logger.error("", e);
			throw new DaoException("查找环节ID为：" + actId + " 的催办信息出错！");
		}

		if (query.resultSetAvailable()) {// 检查结果集是否可用
			// 获取并处理记录集
			RsDataSet rs = new RsDataSet(query.getSqlResultSet());
			if (rs.rowCount > 0) {
				urgency.setId(rs.getFieldValueAsString("id"));
				urgency.setProcId(rs.getFieldValueAsString("proc_id"));
				urgency.setActId(rs.getFieldValueAsString("act_id"));
				urgency.setUrgeTime(rs.getFieldValueAsString("urge_time"));
				urgency
						.setUrgencyContent(rs
								.getFieldValueAsString("urge_info"));
				urgency.setRemindInfo(rs.getFieldValueAsString("remind_info"));
				urgency.setRepeatCount(rs
						.getFieldValueAsInteger("repeat_count").intValue());
				urgency.setRepeatIntervalDay(rs.getFieldValueAsInteger(
						"repeat_interval_day").intValue());
				urgency.setRepeatIntervalHour(rs.getFieldValueAsInteger(
						"repeat_interval_hour").intValue());
			}
			rs.clearAll();
		}
		return urgency;
	}

}
